﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Hired.Classes
{
    public class DBJobIndustryHits
    {
        //create job industry hits
        public static bool CreateJobIndustryHits(JobIndustryHit jih, JobseekerAccount jsa)
        {
            jih.JobIndustryHitsID = "tempvalue";
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    db.Attach(jsa);
                    jsa.JobIndustryHits.Add(jih);
                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        //retrieve job industry hits
        public static JobIndustryHit RetrieveJobIndustryHits(string jobIndustryHitsID)
        {
            JobIndustryHit jobIndustryHits = null;
            using (hireddbEntities db = new hireddbEntities())
            {
                jobIndustryHits =
                (from jih in db.JobIndustryHits
                 where jih.JobIndustryHitsID == jobIndustryHitsID
                 select jih).SingleOrDefault();
            }
            return jobIndustryHits;
        }

        //retrieve job industry hits by JobseekerAccountID and Industry
        public static JobIndustryHit RetrieveJobIndustryHitsByJAccountIDAndIndustry(string jAccountID, string industry)
        {
            JobIndustryHit jobIndustryHits = null;
            using (hireddbEntities db = new hireddbEntities())
            {
                jobIndustryHits =
                (from jih in db.JobIndustryHits
                 where jih.JAccountID == jAccountID && jih.Industry == industry
                 select jih).SingleOrDefault();
            }
            return jobIndustryHits;
        }

        //update job industry hits
        public static bool UpdateJobIndustryHits(JobIndustryHit jih)
        {
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    JobIndustryHit jobIndustryHitsToUpdate = RetrieveJobIndustryHits(jih.JobIndustryHitsID);
                    db.Attach(jobIndustryHitsToUpdate);
                    jobIndustryHitsToUpdate.JAccountID = jih.JAccountID;
                    jobIndustryHitsToUpdate.Industry = jih.Industry;
                    jobIndustryHitsToUpdate.Count = jih.Count;

                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }

        }

        //delete job industry hits
        public static bool DeleteJobIndustryHits(JobIndustryHit jih)
        {
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    JobIndustryHit deleteJobIndustryHits = RetrieveJobIndustryHits(jih.JobIndustryHitsID);
                    db.Attach(deleteJobIndustryHits);
                    db.DeleteObject(deleteJobIndustryHits);
                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        //check if for this Jobseeker Account, is there a row of JobIndustryHits for this certain industry already
        public static bool CheckJAccountJobIndustryHitsForIndustryExist(string jAccountID, string industry)
        {
            try
            {
                JobIndustryHit jobIndustryHitsObject = null;
                using (hireddbEntities db = new hireddbEntities())
                {
                    jobIndustryHitsObject =
                    (from jih in db.JobIndustryHits
                     where jih.JAccountID == jAccountID && jih.Industry == industry
                     select jih).SingleOrDefault();
                }

                if (jobIndustryHitsObject == null)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        //increase the count of job industry hits for a Jobseeker Account for a certain industry
        public static bool IncrementJobIndustryHit(string JAccountID, string industry)
        {
            try
            {
                bool incremented = false;
                JobIndustryHit jobIndustryHitsToIncreaseCount = RetrieveJobIndustryHitsByJAccountIDAndIndustry(JAccountID, industry);
                jobIndustryHitsToIncreaseCount.Count++;
                incremented = UpdateJobIndustryHits(jobIndustryHitsToIncreaseCount);
                return incremented;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}